package com.dysoft.flesh.ui;

import com.dysoft.bones.Property;
import com.sun.opengl.util.j2d.TextRenderer;

import java.awt.*;
import java.awt.geom.Rectangle2D;

/**
 * @author Sean Micklethwaite
 *         Oct 26, 2010 9:23:14 PM
 */
public class UiLabel implements UiRenderer {
	public static TextRenderer font = new TextRenderer(
			new Font("Arial", 0, 12), true, true
	);
	static {
		font.setUseVertexArrays(false);
	}

	String text;
	Property.Trigger<Rectangle2D> bounds;

	public UiLabel(String text) {
		this.text = text;
		this.bounds = new Property.Trigger<Rectangle2D>(font.getBounds(text));
	}

	public void render(Context ctx) {
		Rectangle2D bounds = ctx.getShape().getBounds();

		font.beginRendering(ctx.getTarget().getWidth(), ctx.getTarget().getHeight());
		font.draw(text, ctx.getOffset().x + (int)bounds.getMinX(),
				ctx.getTarget().getHeight() - (ctx.getOffset().y + (int)bounds.getMinY()
						+ (int)this.bounds.get().getHeight()));
		font.endRendering();
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		this.bounds.set(font.getBounds(text));
	}

	public Property.Trigger<Rectangle2D> getBounds() {
		return bounds;
	}
}
